﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Data;
namespace Server
{
    class CheckClass
    {
     static string psw, path;
        public static string[] checkDatabase(string[] database)
        {


            dataTools data = new dataTools(configClass.databaseip, configClass.databaseusername, "information_schema", configClass.databaseport, configClass.psw);

            Debug.LogAppend(ConsoleColor.Gray, "---" + DateTime.Now.ToString() + "---" + "\r\n");
            Debug.Log("正在检查数据库完整性");

            foreach (var item in database)
            {
                Debug.Log( "检查 \'" + item + "'是否存在");
                object count = null;
                try
                {
                    count = data.searchListCount("information_schema.SCHEMATA", new string[] { "SCHEMA_NAME" }, new string[] { item }).msg;
                    if ((int)count != 0)
                    {
                        if (item == "server")
                        {
                            var serverDate = new dataTools(configClass.databaseip, configClass.databaseusername, "server", configClass.databaseport, configClass.psw);
                            
                            path = (string)serverDate.getTable("serverTable").Rows[0]["path"];
                            psw = (string)serverDate.getTable("serverTable").Rows[0]["psw"];
                            if (!Directory.Exists(path))
                            {
                                Debug.Error("名为“"+path+"”的目录不存在，请重新输入");
                                Debug.Log("请继续填写音乐路径,直接回车进行选择");
                                while (true)
                                {
                                    path = Console.ReadLine();
                                    if (path == "")
                                    {
                                        Debug.Log("回车");
                                        chooseDir();
                                        Debug.Success("目录选择成功，当前目录：" + path);
                                        break;
                                    }
                                    else
                                    {
                                        if (Directory.Exists(path))
                                        {
                                            Debug.Success("目录输入成功，当前目录：" + path);
                                            break;
                                        }
                                        else
                                        {
                                            Debug.Error("目录不存在，请重新输入");
                                        }

                                    }
                                }
                                serverDate.update("serverTable", new string[] {"path", path,"id","1"});
                                
                            }
                        }
                        Debug.Success("存在");
                    }
                    else
                    {

                        Debug.Error("不存在，正在新建'" + item + "'数据库");
                        data = new dataTools(configClass.databaseip, configClass.databaseusername, configClass.databaseport, configClass.psw);
                        count = data.createDataBase(item);

                        Debug.Success("新建'" + item + "'数据库成功");

                        data = new dataTools(configClass.databaseip, configClass.databaseusername, "information_schema", configClass.databaseport, configClass.psw);
                        if (item == "server")
                        {
                            serverCfg();
                            var serverDate = new dataTools(configClass.databaseip, configClass.databaseusername, "server", configClass.databaseport, configClass.psw);
                            serverDate.insert("serverTable", new string[] { "psw","path" }, new string[] { psw,path });
                        }
                    }
                }
                catch (Exception)
                {

                    Debug.Error("错误:" + count.ToString());
                    Debug.Error("修复失败，请检测MySQL服务器是否开启,服务器密码是否为正确，即将退出");

                    Console.ReadKey();
                    System.Environment.Exit(0);

                }
            }


            Debug.Success( "检查完成");
            return new string[] { path, psw };
        }
       
        
        static void chooseDir()
        {
            Thread t = new Thread((ThreadStart)(() =>
            {
                FolderBrowserDialog dlg = new FolderBrowserDialog();

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    path= dlg.SelectedPath.ToString();
                }
                
            } ));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            t.Join();

        }
        static void serverCfg()
        {
            Debug.Log("您还没有填写服务器密码和音乐路径,请输入密码：");

            while (true)
            {
                var psw1 = Debug.password();
                if (psw1.Length < 6)
                {
                    Debug.Error("密码最少六位");
                }
                else
                {
                    Debug.Log("请确认密码：");
                    var psw2 = Debug.password();
                    if (psw1 != psw2)
                    {
                        Debug.Error("两次输入不匹配，请重新输入：");
                    }
                    else
                    {
                        psw = psw1;
                        Debug.Success("密码修改成功");
                        break;
                    }
                }
            }

            Debug.Log("请继续填写音乐路径,直接回车进行选择");
            while (true)
            {
                path = Console.ReadLine();
                if (path == "")
                {
                    Debug.Log("回车");
                    chooseDir();
                    Debug.Success("目录选择成功，当前目录：" + path);
                    break;
                }
                else
                {
                    if (Directory.Exists(path))
                    {
                        Debug.Success("目录输入成功，当前目录：" + path);
                        break;
                    }
                    else
                    {
                        Debug.Error("目录不存在，请重新输入");
                    }

                }
            }

        }
        public static string getPath()
        {
            return path;
        }

        public static string getPsw()
        {
            return psw;
        }

    }
}
